package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.adapter.ProjectXiangmuNameAdapter;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.model.ProjectXiangmuInfoBean;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 项目经验公司名称
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class ProjectXiangmuNameActivity extends RequestActivity {

	LinearLayout llayout;

	ImageView back;
	TextView addcompany, savedata, titletext, del;
	List<ProjectXiangmuInfoBean> messageInfo;

	ProjectXiangmuNameAdapter adapter;

	ListView listview;

	List<ProjectXiangmuInfoBean> Xuanzhongmessage;

	public static ProjectXiangmuInfoBean bean;

	public String comid = "";
	String titlename = "";

	int position = 0;
	boolean isbianji = true;

	boolean xiazaiwanbi = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		messageInfo = new ArrayList<ProjectXiangmuInfoBean>();
		Xuanzhongmessage = new ArrayList<ProjectXiangmuInfoBean>();
		comid = getIntent().getStringExtra("comid");
		titlename = getIntent().getStringExtra("titlename");
		position = getIntent().getIntExtra("position", -1);
		initView();
		launchRequest(WooRequestFactory.getProjectXiangmu(comid));
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {

		del = (TextView) this.findViewById(R.id.del);
		del.setOnClickListener(this);
		titletext = (TextView) this.findViewById(R.id.titletext);
		titletext.setText(titlename);
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);
		listview = (ListView) this.findViewById(R.id.listview);

		savedata = (TextView) this.findViewById(R.id.savedata);
		savedata.setOnClickListener(this);
	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		if (bundle.containsKey(WooRequestFactory.RESPONSE_PROJECTXIANGMU_DATA)) {
			messageInfo = new ArrayList<ProjectXiangmuInfoBean>();
			Xuanzhongmessage = new ArrayList<ProjectXiangmuInfoBean>();
			List<ProjectXiangmuInfoBean> retList = bundle
					.getParcelableArrayList(WooRequestFactory.RESPONSE_PROJECTXIANGMU_DATA);
			System.out.println("retList长度：" + retList.size());
			if (retList != null)
				messageInfo = retList;
			System.out.println("messageInfo长度：" + messageInfo.size());
			ProjectXiangmuInfoBean pfib = new ProjectXiangmuInfoBean();
			pfib.setProjectName("");
			// pfib.setProjectCount(0);
			pfib.setIsselected(0);
			messageInfo.add(pfib);
			System.out.println("长度：" + messageInfo.size());
			adapter = new ProjectXiangmuNameAdapter(this, messageInfo,
					Xuanzhongmessage);
			listview.setAdapter(adapter);

			xiazaiwanbi = true;

		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_UPDATESUCCESS) == 1) {
				launchRequest(WooRequestFactory.getProjectXiangmu(comid));
			} else {
				Toast.makeText(this, "修改失败!", Toast.LENGTH_LONG).show();
			}

		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_DELXIANGMU)) {
			if (bundle.getInt(WooRequestFactory.BUNDLE_EXTRA_DELXIANGMU) == 1) {
				System.out.println("删除成功！");
				// launchRequest(WooRequestFactory.getProjectGongsi());
				for (int i = 0; i < Xuanzhongmessage.size(); i++) {
					if (messageInfo.contains(Xuanzhongmessage.get(i))) {
						messageInfo.remove(Xuanzhongmessage.get(i));
					}
				}
				adapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.projectxiangmuname_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			if (xiazaiwanbi == false)
				finish();
			else
				backActivity();
			break;
		case R.id.savedata:// 保存
			if (isbianji) {
				isbianji = false;
				savedata.setText("保存");
				if (adapter != null) {
					adapter.showButton();
					del.setVisibility(View.VISIBLE);
				}
			} else {
				isbianji = true;
				savedata.setText("编辑");
				if (adapter != null) {
					adapter.hiddenButton();
					del.setVisibility(View.GONE);
				}
			}

			break;
		case R.id.del:
			if (Xuanzhongmessage.size() > 0) {
				String deleteUserExpCom = Xuanzhongmessage.get(0).getId();
				for (int i = 1; i < Xuanzhongmessage.size(); i++) {
					deleteUserExpCom += "," + Xuanzhongmessage.get(i).getId();
				}
				launchRequest(WooRequestFactory.delXiangmu(deleteUserExpCom));
			}

			break;

		default:
			break;
		}
	}

	private void backActivity() {
		Intent intent = new Intent();
		intent.putExtra("position", position);
		intent.putExtra("size", messageInfo.size());
		setResult(110, intent);
		finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (xiazaiwanbi == false)
				finish();
			else
				backActivity();
			return true;

		}

		return super.onKeyDown(keyCode, event);

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) { // resultCode为回传的标记，我在B中回传的是RESULT_OK

		case 99:
			launchRequest(WooRequestFactory.getProjectXiangmu(comid));
			isbianji = true;
			savedata.setText("编辑");
			if (adapter != null) {
				adapter.hiddenButton();
				del.setVisibility(View.GONE);
			}
			break;
		case 10010:
			String name = data.getStringExtra("name");
			// String nameid = data.getStringExtra("nameid");

			// if (!nameid.equals("")) {
			// launchRequest(WooRequestFactory.addGongsi("comID", nameid));
			// } else {
			ProjectXiangmuInfoBean bean = new ProjectXiangmuInfoBean();
			bean.setProjectName(name);
			bean.setProjectExpComID(comid);

			launchRequest(WooRequestFactory.addXiangmu(bean));
			// }
			break;
		default:
			break;
		}
	}
}
